package com.minidb.sql_compiler;

import com.minidb.utils.Condition;
import com.minidb.utils.StatementType;

import java.util.List;

public class SQLStatement {
    public StatementType type;
    public String tableName;
    public List<String> columns;
    public List<String> columnTypes;
    public List<String> values;
    public Condition condition;

    // 语句来源 token，用于错误定位（可选）
    public Token originToken;

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        switch (type) {
            case SELECT -> {
                sb.append("SELECT ");
                sb.append(columns==null || columns.isEmpty() ? "*" : String.join(",", columns));
                sb.append(" FROM ").append(tableName);
                if (condition!=null) sb.append(" WHERE ").append(condition);
            }
            case INSERT -> {
                sb.append("INSERT INTO ").append(tableName);
                if (columns!=null && !columns.isEmpty()) sb.append("(").append(String.join(",",columns)).append(")");
                sb.append(" VALUES(").append(values==null?"":String.join(",",values)).append(")");
            }
            case CREATE_TABLE -> {
                sb.append("CREATE TABLE ").append(tableName).append(" (");
                if (columns!=null && columnTypes!=null) {
                    for (int i=0;i<columns.size();i++) {
                        sb.append(columns.get(i)).append(" ").append(columnTypes.get(i));
                        if (i<columns.size()-1) sb.append(", ");
                    }
                }
                sb.append(")");
            }
            case DELETE -> {
                sb.append("DELETE FROM ").append(tableName);
                if (condition!=null) sb.append(" WHERE ").append(condition);
            }
        }
        return sb.toString();
    }
}
